﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Avatar.Company;
using Avatar.Common;
using Avatar.MembershipService;
using AvatarHelper;

public partial class CompanyHome : System.Web.UI.Page
{
    public static string goldCount = "0", _fwTime, isLookGold;

    protected void Page_Load(object sender, EventArgs e)
    {
        var _user = LoginManager.GetCurrentUser();
        if (_user != null)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                if (!IsPostBack)
                {
                    var _companyService = _adc.CompanyService.FirstOrDefault(c => c.Company == _user.UserName);
                    if (_companyService == null)
                    {
                        lbl_Vip.Text = "普通企业服务";
                    }
                    else
                    {
                        lbl_Vip.Text = _adc.CompanyServiceType.FirstOrDefault(t => t.ID == _companyService.ServiceTypeID).TypeName;
                    }

                    string _str = string.Empty;

                    var _information = _adc.CompanyInformation.FirstOrDefault(i => i.User == _user.UserName);

                    if (_information == null) Helper.ShowMassage("您的企业基本资料尚未填写，接下来请填写您企业的基本资料。", "Company_Information.aspx");
                    if (_information != null&&_information.CompanyContact == null) Helper.ShowMassage("您的企业联系信息尚未填写，接下来请填写您企业的联系信息。", "Company_Contact.aspx");

                    if (_information != null)
                    {
                        lbl_UpdateTime.Text = _information.UpdateTime == null ? "--" : _information.UpdateTime.ToString();
                        lbl_Hits.Text = _information.Hits.ToString(); // 企业被查看总数

                        #region 发布的职位数
                        lbl_JobCount.Text = JobManager.GetJobList(_user.UserName, 0).Count().ToString();
                        #endregion
                        var _account = _adc.CompanyFinancialAccount.FirstOrDefault(a=>a.Company==_user.UserName);
                        lbl_Belance.Text = _account == null ? "0.00" : _account.Balance.ToString();
                    }
                }
            }
        }
    }
    protected void btn_Refresh_Click(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            var _user = LoginManager.GetCurrentUser();
            if (_user != null)
            {
                var _company = _adc.CompanyInformation.FirstOrDefault(i => i.User == _user.UserName);
                if (_company != null)
                {
                    _company.UpdateTime = DateTime.Now;
                    _adc.SubmitChanges();
                    lbl_UpdateTime.Text = _company.UpdateTime.ToString();
                }
            }
        }
        Helper.ShowMassage("更新成功！");
    }
}
